home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Creator Changer 2.7 / Code & Resource / Creator Changer.start.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-27  |  14.2 KB  |  594 lines  |  [TEXT/KAHL]

  1. /**********************************************************************
  2.  *    This file contains the functions which start off the program.  The
  3.  *    main event loop and Macintosh toolbox definitions are in this
  4.  *    file.
  5.  **********************************************************************/
  6.  
  7. #include    "Creator Changer.h"
  8. #include    "Creator Changer.start.h"
  9.  
  10.  
  11. /**********************************************************************
  12.  *    Function main(), this is the main function of the program.  The 
  13.  *    major parts of the program are called in this function.
  14.  **********************************************************************/
  15.  
  16. void main(void)
  17.     {
  18.     
  19.     Init_Toolbox();
  20.     Get_Strings();
  21.     Check_Sys_Type();
  22.     Install_AE_Handlers();
  23.     Set_Up_Menu_Bar();
  24.     Open_Preferences();
  25.     
  26.     while(!All_Done) Handle_One_Event();
  27.  
  28.     }
  29.     
  30.  
  31. /**********************************************************************
  32.  *    Function Handle_One_Event(), this function handles one event.  
  33.  *    Depending on what the event is, it gets directed to the 
  34.  *    appropriate place.
  35.  **********************************************************************/
  36.  
  37. void Handle_One_Event(void) 
  38.     {
  39.     
  40.     EventRecord        the_event;
  41.     
  42.     if(WaitNextEvent(everyEvent, &the_event, Sleep_Ticks, MOUSE_REGION))
  43.         {    
  44.         if(IsDialogEvent(&the_event)) Handle_Dialog_Event(&the_event);
  45.         switch(the_event.what)
  46.             {
  47.             case mouseDown:
  48.                 Handle_Mouse_Down(&the_event);
  49.                 break;
  50.             case keyDown:
  51.             case autoKey:
  52.                 {
  53.                 static char    command_key;
  54.                 
  55.                 Maintain_Menu_Items(FrontWindow());
  56.                 command_key=the_event.message & charCodeMask;
  57.                 if(the_event.modifiers & cmdKey) (void)Handle_Menu_Choice(MenuKey(command_key));
  58.                 }
  59.             case app4Evt:
  60.                 if(the_event.message & 0x01000000)
  61.                     {
  62.                     if(the_event.message & 0x00000001)
  63.                         {
  64.                         In_Background=FALSE;
  65.                         SetCursor(&qd.arrow);
  66.                         Sleep_Ticks=5;
  67.                         }
  68.                     else
  69.                         {
  70.                         In_Background=TRUE;
  71.                         Sleep_Ticks=150;
  72.                         }
  73.                     }
  74.                 break;
  75.             case kHighLevelEvent:
  76.                 Option_Key_Down=Is_Pressed(58);
  77.                 AEProcessAppleEvent(&the_event);
  78.                 break;
  79.             }
  80.         }
  81.     else Do_Dialog_Null_Event(&the_event);
  82.  
  83.     
  84.     }
  85.  
  86.  
  87.  
  88. /**********************************************************************
  89.  *    Function Handle_Dialog_Event(), this function sends events to the dialog event 
  90.  *    loop, then if the event loop did not handle the event it then checks to see what was
  91.  *    was selected.
  92.  **********************************************************************/
  93.  
  94. void Handle_Dialog_Event(EventRecord *the_event)
  95.     {    
  96.     
  97.     if(Dialog_Event_Loop(the_event))
  98.         {
  99.         DialogPtr    the_dialog;
  100.         short        the_item;
  101.     
  102.         if(DialogSelect(the_event, &the_dialog, &the_item))
  103.             {
  104.             if(the_dialog==Chng.dialog)    Switch_Chng_Dialog(&Chng, the_item);
  105.             else if(the_dialog==About.dialog) Switch_Abot_Dialog(&About, the_item);
  106.             else if(the_dialog==Pref.dialog) Switch_Pref_Dialog(&Pref, the_item);
  107.             }
  108.         }
  109.         
  110.     }
  111.  
  112.  
  113.  
  114. /**********************************************************************
  115.  *    Function Handle_Mouse_Down(), this function handles any mouse down
  116.  *    event, such as a window select, in the menu bar etc.
  117.  **********************************************************************/
  118.  
  119. void Handle_Mouse_Down(EventRecord *the_event)
  120.     {
  121.     
  122.     short        the_part;
  123.     long        menu_choice, the_volume;
  124.     Rect        drag_rect;
  125.     RgnHandle    the_gray_rgn;
  126.     WindowPtr    which_window;
  127.     DialogPtr    the_dialog=FrontWindow();
  128.                 
  129.     GetSysBeepVolume(&the_volume);
  130.     SetSysBeepVolume(the_volume);
  131.     the_part=FindWindow(the_event->where, &which_window);
  132.     the_gray_rgn=GetGrayRgn();  
  133.     
  134.     switch(the_part)
  135.         {
  136.         case inMenuBar:
  137.             Maintain_Menu_Items(the_dialog);
  138.             menu_choice=MenuSelect(the_event->where);
  139.             (void)Handle_Menu_Choice(menu_choice);              
  140.             break;
  141.         case inSysWindow: 
  142.             SystemClick(the_event, which_window);
  143.             break;
  144.         case inDesk:
  145.         case inContent:
  146.             if(which_window!=the_dialog) SysBeep(the_volume);
  147.             break;
  148.         case inDrag:
  149.             if(which_window==the_dialog) DragWindow(which_window, the_event->where, &((**the_gray_rgn).rgnBBox));
  150.             else SysBeep(the_volume);
  151.         default:
  152.             break;
  153.         }
  154.  
  155.     }
  156.  
  157.  
  158.  
  159. /**********************************************************************
  160.  *    Function Handle_Menu_Choice(), this function handles the menu 
  161.  *    choice made, then it directs it to the appropriate place.
  162.  **********************************************************************/
  163.  
  164. Boolean Handle_Menu_Choice(long menu_choice)
  165.     {
  166.     
  167.     short    the_menu=HiWord(menu_choice);
  168.     short    the_item=LoWord(menu_choice);
  169.     
  170.     if(menu_choice!=0)
  171.         {
  172.         switch(the_menu)
  173.             {
  174.             case APPLE_MENU_ID:
  175.                 Handle_Apple_Choice(the_item);
  176.                 HiliteMenu(0);
  177.                 return(TRUE);
  178.                 break;
  179.             case FILE_MENU_ID:
  180.                 Handle_File_Choice(the_item);
  181.                 HiliteMenu(0);
  182.                 return(TRUE);
  183.                 break;
  184.             case EDIT_MENU_ID:
  185.                 Handle_Edit_Choice(the_item);
  186.                 HiliteMenu(0);
  187.                 return(TRUE);
  188.             default:
  189.                 return(FALSE);
  190.                 break;
  191.             }
  192.         }
  193.         
  194.     }
  195.  
  196.  
  197.  
  198. /**********************************************************************
  199.  *    Function Is_Pressed(), this function determine what key is pressed.
  200.  **********************************************************************/
  201.  
  202. Boolean Is_Pressed(unsigned short key)
  203.     {
  204.     
  205.     unsigned char key_mask[16];
  206.  
  207.     GetKeys((unsigned long *)key_mask);
  208.     return((key_mask[key>>3]>>(key&7))&1);
  209.     
  210.     }
  211.  
  212.  
  213.  
  214. /**********************************************************************
  215.  *    Function  Do_Dialog_Null_Event(), this function keeps the  text edit cursor blinking
  216.  *    and makes sure that the arrow changes to an I beam when ove the edit boxes.
  217.  **********************************************************************/
  218.  
  219. void Do_Dialog_Null_Event(EventRecord *the_event)
  220.     {
  221.     
  222.     DialogPtr    the_dialog=FrontWindow();
  223.     GrafPtr        old_port;
  224.     
  225.     GetPort(&old_port);
  226.     SetPort(the_dialog);
  227.     
  228.     if(the_dialog!=NIL_PTR)
  229.         {
  230.         if(!In_Background)
  231.             {
  232.             short        the_item;
  233.             
  234.             if(Check_Edit_Box(the_dialog))
  235.                 {
  236.                 CursHandle    the_cursor;
  237.             
  238.                 the_cursor=GetCursor(iBeamCursor);
  239.                 HLock((Handle)the_cursor);
  240.                 SetCursor(*the_cursor);
  241.                 HUnlock((Handle)the_cursor);
  242.                 }
  243.             else SetCursor(&qd.arrow);
  244.  
  245.             DialogSelect(the_event, &the_dialog, &the_item);
  246.             }
  247.         }
  248.     SetPort(old_port);
  249.     
  250.     }
  251.  
  252.  
  253.     
  254. /**********************************************************************
  255.  *    Function  Dialog_Event_Loop(), this function is the switch for 
  256.  *    any of the dialog events.
  257.  **********************************************************************/
  258.  
  259. Boolean Dialog_Event_Loop(EventRecord *the_event)
  260.     {
  261.     
  262.     switch(the_event->what)
  263.         {
  264.         case keyDown:
  265.             Maintain_Menu_Items(FrontWindow());
  266.             Handle_Key_Down(the_event);
  267.             return(FALSE);
  268.             break;
  269.         default:
  270.             return(TRUE);
  271.             break;
  272.         }
  273.     
  274.     }
  275.  
  276.  
  277.  
  278. /**********************************************************************
  279.  *    Function Check_Edit_Box(), this function  checks to see if the cursor
  280.  *    is in any of the text-edit boxes in any of the dialogs.
  281.  **********************************************************************/
  282.  
  283. Boolean Check_Edit_Box(DialogPtr the_dialog)
  284.     {
  285.     
  286.     Point        the_point;
  287.     short        os=Pref.num_items;
  288.     
  289.     GetMouse(&the_point);
  290.     
  291.     if(the_dialog==Chng.dialog)
  292.         {
  293.         if(Get_Dialog_Item_In_Rect(the_dialog, CHNG_CREATOR, the_point)) return(YES);
  294.         if(Get_Dialog_Item_In_Rect(the_dialog, CHNG_FILE, the_point)) return(YES);
  295.         }
  296.     else if(the_dialog==Pref.dialog)
  297.         {
  298.         if(Pref.menu_id==P_EPTYPE_ITEM)
  299.             {
  300.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_PT_CT, the_point)) return(YES);
  301.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_PT_FT, the_point)) return(YES);
  302.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_PT_DS, the_point)) return(YES);
  303.             }
  304.         else if(Pref.menu_id==P_EATYPE_ITEM)
  305.             {
  306.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_AC_CHFT, the_point)) return(YES);
  307.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_AC_CTCT, the_point)) return(YES);
  308.             if(Get_Dialog_Item_In_Rect(the_dialog, os+P_AC_CTFT, the_point)) return(YES);
  309.             }
  310.         }
  311.     return(NO);
  312.     
  313.     }
  314.  
  315.  
  316.  
  317. /**********************************************************************
  318.  *    Function  Handle_Key_Down(), this function handles any key down event
  319.  *    when any of the dialog boxes are open.
  320.  **********************************************************************/
  321.  
  322. void Handle_Key_Down(EventRecord *the_event)
  323.     {
  324.     
  325.     static char    key_pressed;
  326.     short            os=Pref.num_items;
  327.     DialogPtr        the_dialog=FrontWindow();
  328.     
  329.     key_pressed=the_event->message & charCodeMask;
  330.     if(!(the_event->modifiers & cmdKey))
  331.         {
  332.         if(the_dialog==Chng.dialog) switch(key_pressed)
  333.             {
  334.             case Escape_Key:
  335.                 Handle_Key_Pressed(Chng.dialog, CHNG_CANCEL);
  336.                 Switch_Chng_Dialog(&Chng, CHNG_CANCEL);
  337.                 break;
  338.             case Return_Key:
  339.             case Enter_Key:
  340.                 Handle_Key_Pressed(Chng.dialog, CHNG_OK);
  341.                 Switch_Chng_Dialog(&Chng, CHNG_OK);
  342.                 break;
  343.             default:
  344.                 {
  345.                 short    the_item;
  346.                 
  347.                 DialogSelect(the_event, &the_dialog, &the_item);
  348.                 }
  349.                 break;
  350.             }
  351.         else if(the_dialog==About.dialog || the_dialog==Pref.dialog) switch(key_pressed)
  352.             {
  353.             case Escape_Key:
  354.             case Return_Key:
  355.             case Enter_Key:
  356.                 if(the_dialog==About.dialog)
  357.                     {
  358.                     Handle_Key_Pressed(About.dialog, ABOUT_OK);
  359.                     Switch_Abot_Dialog(&About, ABOUT_OK);
  360.                     }
  361.                 else if(the_dialog==Pref.dialog)
  362.                     if(Pref.menu_id==P_EPTYPE_ITEM)
  363.                         {
  364.                         Handle_Key_Pressed(Pref.dialog, os+P_PT_DON);
  365.                         Switch_Pt_Dialog(&Pref, os+P_PT_DON);
  366.                         }
  367.                     if(Pref.menu_id==P_EATYPE_ITEM)
  368.                         {
  369.                         Handle_Key_Pressed(Pref.dialog, os+P_AC_DON);
  370.                         Switch_Ac_Dialog(&Pref, os+P_AC_DON);
  371.                         }
  372.                     else if(Pref.menu_id==P_GENERL_ITEM)
  373.                         {
  374.                         Handle_Key_Pressed(Pref.dialog, Pref.num_items+P_GN_OKBT);
  375.                         Switch_Gnrl_Dialog(&Pref, Pref.num_items+P_GN_OKBT);
  376.                         }
  377.                 break;
  378.             default:
  379.                 {
  380.                 short    the_item;
  381.                 
  382.                 DialogSelect(the_event, &the_dialog, &the_item);
  383.                 }
  384.                 break;
  385.             }
  386.         PT_Item_To_Edit=AC_Item_To_Edit=0;
  387.         }
  388.     else if(the_event->modifiers & cmdKey)
  389.         if(!Handle_Menu_Choice(MenuKey(key_pressed))) Do_DLOG_Cmd_Key(the_dialog, key_pressed);
  390.     
  391.     }
  392.  
  393.  
  394.  
  395. /**********************************************************************
  396.  *    Function  Do_DLOG_Cmd_Key(), this function handles a command key
  397.  *    event for any of the open dialogs.
  398.  **********************************************************************/
  399.  
  400. void Do_DLOG_Cmd_Key(DialogPtr the_dialog, char key_pressed)
  401.     {
  402.     
  403.     short        os=Pref.num_items;
  404.     
  405.     if(the_dialog==Chng.dialog) switch(key_pressed)
  406.         {
  407.         case 'M':
  408.         case 'm':
  409.             Handle_Key_Pressed(Chng.dialog, CHNG_MKLK);
  410.             Switch_Chng_Dialog(&Chng, CHNG_MKLK);
  411.             break;
  412.         case '.':
  413.             Handle_Key_Pressed(Chng.dialog, CHNG_CANCEL);
  414.             Switch_Chng_Dialog(&Chng, CHNG_CANCEL);
  415.             break;
  416.         }
  417.     else if(the_dialog==Pref.dialog)
  418.         {
  419.         if(Pref.menu_id==P_EPTYPE_ITEM) switch(key_pressed)
  420.             {
  421.             case 'D':
  422.             case 'd':
  423.                 Handle_Key_Pressed(Pref.dialog, os+P_PT_DEL);
  424.                 Switch_Pt_Dialog(&Pref, os+P_PT_DEL);
  425.                 break;
  426.             case 'A':
  427.             case 'a':
  428.                 Handle_Key_Pressed(Pref.dialog, os+P_PT_ADD);
  429.                 Switch_Pt_Dialog(&Pref, os+P_PT_ADD);
  430.                 break;
  431.             case 'G':
  432.             case 'g':
  433.                 Handle_Key_Pressed(Pref.dialog, os+P_PT_GFL);
  434.                 Switch_Pt_Dialog(&Pref, os+P_PT_GFL);
  435.                 break;
  436.             }
  437.         else if(Pref.menu_id==P_EATYPE_ITEM) switch(key_pressed)
  438.             {
  439.             case 'D':
  440.             case 'd':
  441.                 Handle_Key_Pressed(Pref.dialog, os+P_AC_DEL);
  442.                 Switch_Ac_Dialog(&Pref, os+P_AC_DEL);
  443.                 break;
  444.             case 'A':
  445.             case 'a':
  446.                 Handle_Key_Pressed(Pref.dialog, os+P_AC_ADD);
  447.                 Switch_Ac_Dialog(&Pref, os+P_AC_ADD);
  448.                 break;
  449.             case 'G':
  450.             case 'g':
  451.                 Handle_Key_Pressed(Pref.dialog, os+P_AC_GFL);
  452.                 Switch_Ac_Dialog(&Pref, os+P_AC_GFL);
  453.                 break;
  454.             }
  455.         else if(Pref.menu_id==P_GENERL_ITEM) switch(key_pressed)
  456.             {
  457.             case 'E':
  458.             case 'e':
  459.                 Handle_Key_Pressed(Pref.dialog, Pref.num_items+P_GN_OKAC);
  460.                 Switch_Gnrl_Dialog(&Pref, Pref.num_items+P_GN_OKAC);
  461.                 break;
  462.             case 'Q':
  463.             case 'q':
  464.                 Handle_Key_Pressed(Pref.dialog, Pref.num_items+P_GN_QADD);
  465.                 Switch_Gnrl_Dialog(&Pref, Pref.num_items+P_GN_QADD);
  466.                 break;
  467.             }
  468.         }
  469.         PT_Item_To_Edit=AC_Item_To_Edit=0;
  470.  
  471.     }
  472.  
  473.     
  474.  
  475. /**********************************************************************
  476.  *    Function Handle_Apple_Choice(), this function decides what item 
  477.  *    to execute under the apple menu.
  478.  **********************************************************************/
  479.  
  480. void Handle_Apple_Choice(short the_item)
  481.     {
  482.     
  483.     short    desk_acc_number;
  484.     Str32    desk_acc_name;
  485.     
  486.     switch(the_item)
  487.         {
  488.         case A_ABOUT_ITEM:
  489.             Open_DLOG(&About);
  490.             break;
  491.         default:
  492.             GetItem(Apple_Menu, the_item, desk_acc_name);
  493.             desk_acc_number=OpenDeskAcc(desk_acc_name);
  494.             break;
  495.         }
  496.     
  497.     }
  498.  
  499.  
  500.  
  501. /**********************************************************************
  502.  *    Function Handle_File_Choice(), this function decides what item
  503.  *    to execute under the options menu.
  504.  **********************************************************************/
  505.  
  506. void Handle_File_Choice(short the_item)
  507.     {
  508.     
  509.     switch(the_item)
  510.         {
  511.         case F_OPEN_ITEM:
  512.             if(Pick_File(&The_File_Spec, &File_Info, TRUE))
  513.                 {
  514.                 Multiple_Files=NO;
  515.                 Chng.num_files=1;
  516.                 Open_DLOG(&Chng);
  517.                 }
  518.             Maintain_Menu_Items(FrontWindow());
  519.             break;
  520.         case F_PREF_ITEM:
  521.             Open_DLOG(&Pref);
  522.             break;
  523.         case F_QUIT_ITEM:
  524.             All_Done=TRUE;
  525.             break;
  526.         default:
  527.             break;
  528.         }
  529.     
  530.     }
  531.     
  532.     
  533.     
  534. /**********************************************************************
  535.  *    Function Handle_Edit_Choice(), this function decides what item
  536.  *    to execute under the edit menu.
  537.  **********************************************************************/
  538.     
  539. void Handle_Edit_Choice(short the_item)
  540.     {
  541.     
  542.     DialogPtr    the_dialog=FrontWindow();
  543.     
  544.     switch(the_item)
  545.         {
  546.         case E_CUT_ITEM:
  547.             HiliteMenu(EDIT_MENU_ID);
  548.             ZeroScrap();
  549.             DlgCopy(the_dialog);
  550.             DlgCut(the_dialog);
  551.             TEToScrap();
  552.             break;
  553.         case E_COPY_ITEM:
  554.             HiliteMenu(EDIT_MENU_ID);
  555.             ZeroScrap();
  556.             DlgCopy(the_dialog);
  557.             TEToScrap();
  558.             break;
  559.         case E_PASTE_ITEM:
  560.             TEFromScrap();
  561.             HiliteMenu(EDIT_MENU_ID);
  562.             DlgPaste(the_dialog);
  563.             break;
  564.         case E_CLEAR_ITEM:
  565.             HiliteMenu(EDIT_MENU_ID);
  566.             DlgDelete(the_dialog);
  567.             break;
  568.         }
  569.     PT_Item_To_Edit=AC_Item_To_Edit=0;
  570.         
  571.     }
  572.  
  573.  
  574.  
  575. /**********************************************************************
  576.  *    Function Handle_Key_Pressed(), this function handels what happens 
  577.  *    if a key is pressed.
  578.  **********************************************************************/
  579.  
  580. static void Handle_Key_Pressed(DialogPtr the_dialog, short the_item)
  581.     {
  582.     
  583.     Handle    item_handle;
  584.     short    item_type;
  585.     long    delay;
  586.     Rect    item_rect;
  587.     
  588.     Get_Dialog_Item_Hndl(the_dialog, the_item, &item_handle);
  589.     HiliteControl((ControlHandle)item_handle, YES);
  590.     Delay(DELAY, &delay);
  591.     HiliteControl((ControlHandle)item_handle, NO);
  592.     
  593.     }
  594.